<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/dute_favicon_32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/dute_favicon_16x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <link rel="manifest" href="/images/manifest.json">
  <meta name="msapplication-config" content="/images/browserconfig.xml">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="google-site-verification" content="mpI5dkydstZXl6UcDCppqktXK0bbvqdZ6LkZ3KNk4Iw">
  <meta name="baidu-site-verification" content="code-a1LksZX2Ds">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"whitestore.top","root":"/","scheme":"Gemini","version":"7.8.0","exturl":true,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="命令模式解耦了请求发送者和接受者">
<meta property="og:type" content="article">
<meta property="og:title" content="浅谈设计模式 - 命令模式（七）">
<meta property="og:url" content="https://whitestore.top/2021/03/07/command/index.html">
<meta property="og:site_name" content="爱看书的阿东">
<meta property="og:description" content="命令模式解耦了请求发送者和接受者">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210222141114.png">
<meta property="og:image" content="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210224115250.png">
<meta property="og:image" content="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210222141114.png">
<meta property="og:image" content="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210126230854.png">
<meta property="og:image" content="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210222141114.png">
<meta property="article:published_time" content="2021-03-07T09:03:43.000Z">
<meta property="article:modified_time" content="2023-07-16T06:28:09.262Z">
<meta property="article:author" content="阿东">
<meta property="article:tag" content="命令模式">
<meta property="article:tag" content="设计模式">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210222141114.png">

<link rel="canonical" href="https://whitestore.top/2021/03/07/command/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>浅谈设计模式 - 命令模式（七） | 爱看书的阿东</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="爱看书的阿东" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">爱看书的阿东</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">赐他一块白色石头，石头上写着新名</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i>站点地图</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL2xhenlUaW1lcw==" title="Follow me on GitHub" aria-label="Follow me on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://whitestore.top/2021/03/07/command/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="阿东">
      <meta itemprop="description" content="随遇而安">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="爱看书的阿东">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          浅谈设计模式 - 命令模式（七）
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-03-07 17:03:43" itemprop="dateCreated datePublished" datetime="2021-03-07T17:03:43+08:00">2021-03-07</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2023-07-16 14:28:09" itemprop="dateModified" datetime="2023-07-16T14:28:09+08:00">2023-07-16</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" itemprop="url" rel="index"><span itemprop="name">设计模式</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/03/07/command/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/03/07/command/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>5.1k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>5 分钟</span>
            </span>
            <div class="post-description">命令模式解耦了请求发送者和接受者</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="浅谈设计模式-命令模式（七）"><a href="#浅谈设计模式-命令模式（七）" class="headerlink" title="浅谈设计模式 - 命令模式（七）"></a>浅谈设计模式 - 命令模式（七）</h1><h1 id="前言："><a href="#前言：" class="headerlink" title="前言："></a>前言：</h1><p>命令模式也是一种比较常见的行为型模式，可以想象我们的手机智能遥控器，通过按动按钮的形式开启各种家具，说白了，就是将一系列的请求命令封装起来，不直接调用真正执行者的方法，这样比较好扩展。需要注意的是命令模式和策略模式相似，所以有时候可能容易弄混，这篇文章将会详细介绍命令模式。</p>
<a id="more"></a>

<h1 id="文章目的："><a href="#文章目的：" class="headerlink" title="文章目的："></a>文章目的：</h1><ol>
<li>了解命令的模式的特点</li>
<li>简单对比命令模式和策略模式</li>
<li>命令模式的优缺点总结</li>
</ol>
<h1 id="什么是命令模式？"><a href="#什么是命令模式？" class="headerlink" title="什么是命令模式？"></a>什么是命令模式？</h1><p>解释：把“请求”封装为对应的对象，使用不同的请求参数化对象，命令模式支持撤销撤销的操作</p>
<p>命令模式是一种行为型模式，实现了接口调用对象和返回对象，用命令对象作为桥梁实现调用者和具体实现者之间的解耦和交互。</p>
<h1 id="命令模式的特点："><a href="#命令模式的特点：" class="headerlink" title="命令模式的特点："></a>命令模式的特点：</h1><ul>
<li>将发出请求的对象和执行请求的对象解耦</li>
<li>调用者可以自由定义命令参数进行自由的组合</li>
<li>命令可以用来实现日志或者<strong>事务系统</strong>（undo操作）</li>
</ul>
<h1 id="命令模式结构图："><a href="#命令模式结构图：" class="headerlink" title="命令模式结构图："></a>命令模式结构图：</h1><p>下面根据命令模式的定义，以及上面对于命令模式的理解，构建具体的结构图</p>
<p><img src="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210222141114.png" alt=""></p>
<p>+ <strong>Client 客户端：</strong>客户端需要创建具体的命令类，并且通过发送请求给执行者调用具体的对象，发送方和接收方不存在关联，统一由命令对象进行连接。</p>
<p>+ <strong>Invoker 执行者</strong>：请求的发送者，负责将请求分发给具体的命令实现类，由实现类调用实际的执行者进行执行操作</p>
<p>+ <strong>Command 接口：</strong>命令接口，定义命令的规范</p>
<p>+ <strong>ConcreteCommand 命令接口实现类</strong>：实现命令的同时组合具体对象。</p>
<p>+ <strong>ConcreteObject 具体实现类</strong>：定义截图的实现生产对象。</p>
<p>+ <strong>Receive 执行者</strong>：请求的真正执行者，可以是任意对象，通常以 <strong>组合</strong>形式出现在执行者的内部</p>
<h1 id="命令模式的理解"><a href="#命令模式的理解" class="headerlink" title="命令模式的理解"></a>命令模式的理解</h1><p>这里参考<strong>《Head firtst设计模式》</strong>的案例，模拟具体的交互流程</p>
<h2 id="对象村餐厅交互过程"><a href="#对象村餐厅交互过程" class="headerlink" title="对象村餐厅交互过程"></a>对象村餐厅交互过程</h2><p>我们到餐厅点餐，一般会经历如下的流程</p>
<ol>
<li><p>客人负责下订单，由服务员接受订单</p>
</li>
<li><p>服务器接收订单，调用订单柜台的下订单的方法，不需要关注细节</p>
</li>
<li><p>订单柜台通知厨师进行生产</p>
</li>
<li><p>厨师生产订单物品之后，交给服务员上菜</p>
</li>
</ol>
<p>根据上面的步骤利用伪代码的表现如下：</p>
<ul>
<li><code>createCommandObject()</code> 构建命令对象</li>
<li><code>setCommand()</code> 传递命令</li>
<li><code>execute()</code> 命令执行</li>
<li><code>action1()</code>，<code>action2()</code> 执行者实际执行</li>
</ul>
<h2 id="交互流程图"><a href="#交互流程图" class="headerlink" title="交互流程图"></a>交互流程图</h2><p>我们根据上面的交互过程介绍，构建具体的交互流程图，我们可以看到里面有角色：<code>客人</code>、<code>服务员</code>、<code>订单柜台</code>、<code>厨师</code>，他们本身并没有关联，而是通过餐厅的形式彼此产生了具体的关联，同时我们对比上面的结构图，看下对象村餐厅对应的结构图：</p>
<p><img src="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210224115250.png" alt=""></p>
<p>下面根据结构图说一下各种角色的职责：</p>
<p>客人：相当于client客户端，负责指挥服务员进行下单的操作。</p>
<p>服务员：充当请求的发送者，接受客户端的请求，调用下订单的接口到具体的订单柜台，但是不需要关心具体的细节，只具备下订单这一个操作</p>
<p>订单柜台：通过服务员传递的订单，安排厨师执行具体的任务</p>
<p>厨师：根据订单柜台的订单做菜，将结果返回给服务员（或客人）</p>
<p>我们从上面的角色图再来看具体的命令模式定义，可以看到基本都是一一对应的情况。</p>
<p><img src="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210222141114.png" alt=""></p>
<h1 id="命令模式和策略模式的对比"><a href="#命令模式和策略模式的对比" class="headerlink" title="命令模式和策略模式的对比"></a>命令模式和策略模式的对比</h1><p>命令模式和策略模式的结构图有些许的类似，下面我们来对比看一下这两张图的异同：</p>
<p>策略模式结构图：</p>
<p><img src="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210126230854.png" alt="策略模式"></p>
<p>命令模式结构图：</p>
<p><img src="https://gitee.com/lazyTimes/imageReposity/raw/master/img/20210222141114.png" alt="命令模式"></p>
<p>相同点：</p>
<ol>
<li>命令模式通过定义命令规范接口，由子类实现命令的执行细节，策略同样定义策略行为同时用子类实现不同的策略功能</li>
<li>命令模式和策略都解耦了请求的发送者和执行者</li>
</ol>
<p><strong>不同点：</strong></p>
<ol>
<li>命令模式利用了命令组合执行对象的形式执行实现具体实现，而策略模式依靠上下文对象进行切换</li>
<li>策略模式针对某个对象实现不同的策略效果，而命令模式关注请求发送者和实现者之间的业务解耦组合</li>
</ol>
<h1 id="实战"><a href="#实战" class="headerlink" title="实战"></a>实战</h1><h2 id="模拟场景："><a href="#模拟场景：" class="headerlink" title="模拟场景："></a>模拟场景：</h2><pre><code>这次的案例还是模拟《Head First》设计模式的当中对于遥控器遥控电器的一个案例，我们定义如下的内容：</code></pre><p>遥控器：命令的发送方，负责根据不同的操作按钮调用不同的设备工作，生成具体的命令对象调用接口执行具体的命令</p>
<p>命令接口：负责定义命令的实现规范，充当遥控器里面的每一个按钮，对应都有具体的实现</p>
<p>命令实现类：负责实现命令的接口，同时调用具体的实现对象执行命令</p>
<p>实现对象：命令的真正执行者，一般夬在命令实现类的内部，比如电视，灯泡等</p>
<h2 id="不适用设计模式"><a href="#不适用设计模式" class="headerlink" title="不适用设计模式"></a>不适用设计模式</h2><p>在不使用设计模式的情况下，我们通常通过对象组合的形式组合不同的实体对象执行命令，下面通过一些简单的代码说明一下设计的弊端：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 灯泡</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Light</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">on</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"打开灯光"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">off</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"关闭灯光"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 电视机</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Television</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">on</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"打开电视"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">off</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"关闭电视"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 遥控器</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RemoteControl</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Light light;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Television television;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">RemoteControl</span><span class="params">(Light light, Television television)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.light = light;</span><br><span class="line">        <span class="keyword">this</span>.television = television;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">button1</span><span class="params">()</span></span>&#123;</span><br><span class="line">        light.on();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">button2</span><span class="params">()</span></span>&#123;</span><br><span class="line">        television.on();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 单元测试</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Television television = <span class="keyword">new</span> Television();</span><br><span class="line">        Light light = <span class="keyword">new</span> Light();</span><br><span class="line">        RemoteControl remoteControl = <span class="keyword">new</span> RemoteControl(light, television);</span><br><span class="line">        remoteControl.button1();</span><br><span class="line">        remoteControl.button2();</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;<span class="comment">/*运行结果：</span></span><br><span class="line"><span class="comment">打开灯光</span></span><br><span class="line"><span class="comment">打开电视</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<p>从上面的简单代码可以看到，如果我们继续增加电器，同时增加方法，不仅会导致遥控器要随着电器的改动不断改动，同时每次新增一个电器，遥控器要进行类似“注册”的行为，需要将电器<strong>接入</strong>到遥控器，这样显然是不符合逻辑的，因为我们都知道，遥控器是单纯的指挥者，他不参与任何命令的操作细节，同时虽然真正工作的方法是具体对象的方法，但是这种形式类似将电器“塞”到了遥控器的内部执行，这样也是存在问题，我们下面需要修改一下这种严重耦合的设计。</p>
<h2 id="使用命令模式改写："><a href="#使用命令模式改写：" class="headerlink" title="使用命令模式改写："></a>使用命令模式改写：</h2><p>我们按照命令模式的结构图，改写案例，我们需要定义下面的类和对应的接口：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">+ RemoteControl 遥控器</span><br><span class="line">+ Command(接口) 命令规范接口，用于接入到遥控器内部</span><br><span class="line">+ LightCommandConcrete 控制电器的亮灭命令实现</span><br><span class="line">+ SwitchCommandConcrete 控制电器的开关命令实现</span><br><span class="line">+ Light 灯泡</span><br><span class="line">+ Television 电视机</span><br></pre></td></tr></table></figure>

<p>首先，我们定义命令的接口，定义接口的规范方法。然后定义实现子类实现不同命令的操作效果，在命令实现类的内部，我们组合实际执行对象，在接口方法调用实际的对象方法，这样就做到了执行者和发送者之间的解耦。</p>
<p>接着，我们改写控制器，他不在持有任何实际的对象方法，通过组合命令的接口，让客户端传入实现的功能，通过这种方式，遥控器不在需要依赖具体的电器实现调用具体方法，而是关注命令的接口方法，一切的细节都在命令的子类内部。</p>
<p>下面代码是依照命令模式进行的最简单的一个实现。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 命令接口</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Command</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 接口备份</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">execute</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LightCommandConcrete</span> <span class="keyword">implements</span> <span class="title">Command</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Light light = <span class="keyword">new</span> Light();</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">execute</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        light.on();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SwitchCommandConcrete</span> <span class="keyword">implements</span> <span class="title">Command</span></span>&#123;</span><br><span class="line">    <span class="keyword">private</span> Television television = <span class="keyword">new</span> Television();</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">execute</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        television.on();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 遥控器</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RemoteControl</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Command command;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">RemoteControl</span><span class="params">(Command command)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.command = command;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">execute</span><span class="params">()</span></span>&#123;</span><br><span class="line">        command.execute();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Command <span class="title">getCommand</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> command;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setCommand</span><span class="params">(Command command)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.command = command;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        RemoteControl remoteControl = <span class="keyword">new</span> RemoteControl(<span class="keyword">new</span> LightCommandConcrete());</span><br><span class="line">        remoteControl.execute();</span><br><span class="line">        remoteControl.setCommand(<span class="keyword">new</span> SwitchCommandConcrete());</span><br><span class="line">        remoteControl.execute();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>经过上面的代码改造，我们成功上面的代码改造为命令模式的代码，使用设计模式之后，我们将调用者和实际执行者进行了解耦，控制器不需要知道执行的细节，只需要组合自己的命令接口，由客户端指定希望实现的内容，执行相对应的具体命令。</p>
<h2 id="案例的额外扩展："><a href="#案例的额外扩展：" class="headerlink" title="案例的额外扩展："></a>案例的额外扩展：</h2><p>下面是对应案例如何进行后续的扩展，对于这部分内容文章篇幅有限，同时本着不重复造轮子的理念，请阅读<strong>《Head First设计模式》</strong>关于命令模式这一个章节，同时安利一下这本书，非常通俗易懂的讲解设计模式，对于个人的提升帮助很大。</p>
<h3 id="对于上面的设计，如何加入Undo的操作？"><a href="#对于上面的设计，如何加入Undo的操作？" class="headerlink" title="对于上面的设计，如何加入Undo的操作？"></a>对于上面的设计，如何加入<strong>Undo</strong>的操作？</h3><p>Undo是一个很常见的功能，如果想要让Undo的操作集成到案例内部，需要按照如下的步骤进行操作：</p>
<ol>
<li>Command 接口增加<code>Undo</code>的操作，让所有命令支持<code>undo</code></li>
<li>在控制器记录<strong>最后一个命令的执行对象</strong>，记录最后的操作命令，实现控制器支持undo操作</li>
<li>具体Command实现增加对于<code>undo()</code>方法调用，并且根据实际的组合对象调用方法</li>
<li>具体实现类实现<code>undo()</code>操作的具体行为效果。</li>
</ol>
<h3 id="如果undo里面，存在一些变量如何处理？"><a href="#如果undo里面，存在一些变量如何处理？" class="headerlink" title="如果undo里面，存在一些变量如何处理？"></a>如果undo里面，存在一些变量如何处理？</h3><p>在命令的实现类内部，需要增加一个<strong>最后变量值</strong>的记录，用于记录当前最后一步操作的属性和变量</p>
<h3 id="如何做到宏命令？"><a href="#如何做到宏命令？" class="headerlink" title="如何做到宏命令？"></a>如何做到宏命令？</h3><p>实现一个命令类，通过组合<strong>数组</strong>或者<strong>堆栈</strong>组合多个其他命令对象，通过for循环的形式依次调用。</p>
<p>undo也可以使用这种方式进行调用的，但是要注意**调用的顺序相反</p>
<h1 id="命令模式的优缺点："><a href="#命令模式的优缺点：" class="headerlink" title="命令模式的优缺点："></a>命令模式的优缺点：</h1><p>优点：</p>
<ul>
<li>命令模式实现了请求发送方和实现方解耦，不论是发送方还是接收方都不需要</li>
<li>命令模式可以实现不同实现对象的自由组合，通过命令组合可以实现一连串简单功能</li>
</ul>
<p>缺点：</p>
<ul>
<li>和策略模式类似，命令模式很容易造成子类的膨胀</li>
</ul>
<h1 id="总结："><a href="#总结：" class="headerlink" title="总结："></a>总结：</h1><pre><code>命令模式是一种非常常见的设计模式，这种模式更多的关注点是解耦请求的发送方和实现方，命令模式在系统设计中使用还是十分常见的，是一种值得关注的设计模式。</code></pre><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kity@2.0.4/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kityminder-core@1.4.50/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.css">
    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>lazytime
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://whitestore.top/2021/03/07/command/" title="浅谈设计模式 - 命令模式（七）">https://whitestore.top/2021/03/07/command/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLzQuMC96aC1DTg=="><i class="fa fa-fw fa-creative-commons"></i>BY-NC</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" rel="tag"># 设计模式</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/03/07/factorymethod2/" rel="prev" title="浅谈设计模式 - 工厂模式（六）">
      <i class="fa fa-chevron-left"></i> 浅谈设计模式 - 工厂模式（六）
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/03/07/adaptermethod/" rel="next" title="浅谈设计模式 - 适配器模式（八）">
      浅谈设计模式 - 适配器模式（八） <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#浅谈设计模式-命令模式（七）"><span class="nav-number">1.</span> <span class="nav-text">浅谈设计模式 - 命令模式（七）</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#前言："><span class="nav-number">2.</span> <span class="nav-text">前言：</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#文章目的："><span class="nav-number">3.</span> <span class="nav-text">文章目的：</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#什么是命令模式？"><span class="nav-number">4.</span> <span class="nav-text">什么是命令模式？</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#命令模式的特点："><span class="nav-number">5.</span> <span class="nav-text">命令模式的特点：</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#命令模式结构图："><span class="nav-number">6.</span> <span class="nav-text">命令模式结构图：</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#命令模式的理解"><span class="nav-number">7.</span> <span class="nav-text">命令模式的理解</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#对象村餐厅交互过程"><span class="nav-number">7.1.</span> <span class="nav-text">对象村餐厅交互过程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#交互流程图"><span class="nav-number">7.2.</span> <span class="nav-text">交互流程图</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#命令模式和策略模式的对比"><span class="nav-number">8.</span> <span class="nav-text">命令模式和策略模式的对比</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#实战"><span class="nav-number">9.</span> <span class="nav-text">实战</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#模拟场景："><span class="nav-number">9.1.</span> <span class="nav-text">模拟场景：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#不适用设计模式"><span class="nav-number">9.2.</span> <span class="nav-text">不适用设计模式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用命令模式改写："><span class="nav-number">9.3.</span> <span class="nav-text">使用命令模式改写：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#案例的额外扩展："><span class="nav-number">9.4.</span> <span class="nav-text">案例的额外扩展：</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#对于上面的设计，如何加入Undo的操作？"><span class="nav-number">9.4.1.</span> <span class="nav-text">对于上面的设计，如何加入Undo的操作？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#如果undo里面，存在一些变量如何处理？"><span class="nav-number">9.4.2.</span> <span class="nav-text">如果undo里面，存在一些变量如何处理？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#如何做到宏命令？"><span class="nav-number">9.4.3.</span> <span class="nav-text">如何做到宏命令？</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#命令模式的优缺点："><span class="nav-number">10.</span> <span class="nav-text">命令模式的优缺点：</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#总结："><span class="nav-number">11.</span> <span class="nav-text">总结：</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">阿东</p>
  <div class="site-description" itemprop="description">随遇而安</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">239</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">36</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">37</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2xhenlUaW1lcw==" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;lazyTimes"><i class="fa fa-fw fa-github"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="bWFpbHRvOjEwOTc0ODM1MDhAcXEuY29t" title="E-Mail → mailto:1097483508@qq.com"><i class="fa fa-fw fa-envelope"></i>E-Mail</span>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly93d3cuNTJwb2ppZS5jbi9ob21lLnBocD9tb2Q9c3BhY2UmdWlkPTE0OTc3MTgmZG89dGhyZWFkJnZpZXc9bWUmZnJvbT1zcGFjZQ==" title="https:&#x2F;&#x2F;www.52pojie.cn&#x2F;home.php?mod&#x3D;space&amp;uid&#x3D;1497718&amp;do&#x3D;thread&amp;view&#x3D;me&amp;from&#x3D;space">吾爱破解</span>
        </li>
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly9qdWVqaW4uaW0vdXNlci8yOTk5MTIzNDUyNjI2MzY2" title="https:&#x2F;&#x2F;juejin.im&#x2F;user&#x2F;2999123452626366">掘金</span>
        </li>
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly9zZWdtZW50ZmF1bHQuY29tL3UvbGF6eXRpbWVz" title="https:&#x2F;&#x2F;segmentfault.com&#x2F;u&#x2F;lazytimes">思否</span>
        </li>
    </ul>
  </div>

      </div>

      <div class="wechat_OA">
        <span>欢迎关注我的公众号</span>
        <br>
          <!-- 这里添加你的二维码图片 -->
        <img src ="https://adong-picture.oss-cn-shenzhen.aliyuncs.com/adong/wechat_channel.jpg">
      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">阿东</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">2m</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">29:50</span>
</div>
  <div class="powered-by">由 <span class="exturl theme-link" data-url="aHR0cHM6Ly9oZXhvLmlv">Hexo</span> & <span class="exturl theme-link" data-url="aHR0cHM6Ly90aGVtZS1uZXh0Lm9yZw==">NexT.Gemini</span> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : true,
      appId      : 'qMUpEEvBgXaMDD1b0ftgi9xr-gzGzoHsz',
      appKey     : 'UCdfT4Rfih6MO6y8DI4fstf6',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-CN' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
